<!--  -->
<template>
  <div class="">
    <el-row>
      <el-col :span="7" style="padding-right: 10px">
        <div class="grid-content bg-purple">
          <el-card class="box-card">
            <div class="user">
              <el-avatar
                class="avatar"
                v-if="userData.headPic"
                :src="userData.headPic"
              ></el-avatar>
              <el-avatar class="avatar" v-else icon="el-icon-user-solid"></el-avatar>
              <div class="user_info">
                <p class="name">{{ userData.username || userData.fullName }}</p>
                <p class="access">{{ userData.roleName }}</p>
              </div>
            </div>
            <div class="login_info">
              <div class="time">
                <p>
                  上次登录时间: <span>{{ userData.endTime || "-" }}</span>
                </p>
                <br />
                <p>
                  上次登录地点: <span>{{ address || "-" }}</span>
                </p>
              </div>
            </div>
          </el-card>
        </div>
        <div class="grid-content bg-purple">
          <el-card class="box-card">
            <a
              href="http://user.hx.xijia.plus/"
              target="_blank"
              style="font-size: 16px;text-decoration: none; color: rgb(0, 201, 255);"
              >用户端地址:http://user.hx.xijia.plus</a
            >
            <br /><br />
            代做毕业设计 <br /><br />
            实惠！！！！！！ 联系扣扣 1624242879
          </el-card>
        </div>
      </el-col>

      <el-col :span="17">
        <el-card>
          <el-select v-model="type" placeholder="请选择" @change="getLineChart">
            <el-option label="天" :value="1"> </el-option>
            <el-option label="月" :value="2"> </el-option>
          </el-select>
          <!-- 折线图 -->
          <div id="tokens" style="height: 280px"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getIpInfo } from "@/services/user";
import { tokenStatistics } from "@/services/chat";
import * as echarts from "echarts";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      userData: {},
      address: "-",
      type: 1,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getIpInfo() {
      getIpInfo()
        .then((res) => {
          this.address = res.data;
          sessionStorage.setItem("address", this.address);
        })
        .catch((error) => {});
    },
    getLineChart() {
      let myChart = echarts.init(document.getElementById("tokens"));
      tokenStatistics(this.type)
        .then((res) => {
          let x = [];
          let y = [];
          res.data.forEach((i) => {
            x.push(i.time);
            y.push(i.tokens);
          });
          let option = {
            title: {
              text: "AI-TOKENS使用量",
              left: "center",
            },
            xAxis: {
              data: x,
            },
            legend: {
              data: "11",
            },
            tooltip: {
              trigger: "axis",
            },
            yAxis: {},
            series: [
              {
                data: y,
                type: "line",
                smooth: true,
              },
            ],
          };
          myChart.setOption(option);
        })
        .catch((error) => {});
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.userData = JSON.parse(sessionStorage.getItem("userData"));
    this.getLineChart();
    this.getIpInfo();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
.box-card {
  .user {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #cccccc;
    padding-bottom: 20px;
    margin-bottom: 20px;

    .avatar {
      height: 110px;
      width: 110px;
      border-radius: 50%;
    }
  }
}

.adrress {
  margin-top: 10px;
}

.user_info {
  width: 55%;
  margin-left: 30px;

  .name {
    font-size: 34px;
    margin-bottom: 10px;
  }

  .access {
    color: #999999;
  }
}
</style>
